import 'package:flutter/material.dart';

class CourseInfo {
  final String name;
  final DateTime startTime;
  final DateTime endTime;
  final String location;

  CourseInfo({
    required this.name,
    required this.startTime,
    required this.endTime,
    required this.location,
  });

  // 用于从JSON反序列化
  factory CourseInfo.fromJson(Map<String, dynamic> json) {
    return CourseInfo(
      name: json['name'] as String,
      startTime: DateTime.parse(json['startTime'] as String),
      endTime: DateTime.parse(json['endTime'] as String),
      location: json['location'] as String? ?? '',
    );
  }

  // 用于转为JSON
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'startTime': startTime.toIso8601String(),
      'endTime': endTime.toIso8601String(),
      'location': location,
    };
  }
}

class CourseCard extends StatelessWidget {
  final CourseInfo course;

  const CourseCard({
    super.key,
    required this.course,
  });

  @override
  Widget build(BuildContext context) {
    final now = DateTime.now();
    String statusText;
    Color statusColor;

    if (now.isBefore(course.startTime)) {
      final diff = course.startTime.difference(now);
      final hours = diff.inHours;
      final minutes = diff.inMinutes % 60;
      statusText = '距离上课：${hours > 0 ? '$hours小时' : ''}$minutes分钟';
      statusColor = Colors.blue;
    } else if (now.isAfter(course.endTime)) {
      statusText = '已结束';
      statusColor = Colors.grey;
    } else {
      statusText = '正在上课';
      statusColor = Colors.green;
    }

    String startStr =
        "${course.startTime.hour.toString().padLeft(2, '0')}:${course.startTime.minute.toString().padLeft(2, '0')}";
    String endStr =
        "${course.endTime.hour.toString().padLeft(2, '0')}:${course.endTime.minute.toString().padLeft(2, '0')}";

    return Card(
      margin: const EdgeInsets.symmetric(vertical: 8),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              course.name,
              style: const TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            Row(
              children: [
                const Icon(Icons.access_time, size: 16),
                const SizedBox(width: 4),
                Text(
                  '${course.startTime.hour.toString().padLeft(2, '0')}:${course.startTime.minute.toString().padLeft(2, '0')} - '
                  '${course.endTime.hour.toString().padLeft(2, '0')}:${course.endTime.minute.toString().padLeft(2, '0')}',
                ),
              ],
            ),
            if (course.location.isNotEmpty) ...[
              const SizedBox(height: 4),
              Row(
                children: [
                  const Icon(Icons.location_on, size: 16),
                  const SizedBox(width: 4),
                  Text(course.location),
                ],
              ),
            ],
            Text(
              statusText,
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: statusColor),
            ),
          ],
        ),
      ),
    );
  }
}
